/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-08-22 11:11:01
 * @LastEditors: GGB
 * @LastEditTime: 2021-08-22 13:29:41
 */
import 'package:flutter/material.dart';
import 'package:flutter_data_center/controller/visitor_controller.dart';
import 'package:get/get.dart';

import 'components/visitor_count.dart';
import 'components/visitor_list.dart';
import 'components/visitor_menu.dart';

class VisitorScreen extends StatefulWidget {
  const VisitorScreen({Key key}) : super(key: key);
  static String routeName = "/visitor";

  @override
  _VisitorScreenState createState() => _VisitorScreenState();
}

class _VisitorScreenState extends State<VisitorScreen> {
  VisitorController _attendanceController = Get.put(VisitorController());
  final keyWidget = GlobalKey();
  double height = 0;
  DateTime _selectedDate;

  void calculateSizeAndPosition() =>
      WidgetsBinding.instance.addPostFrameCallback((_) {
        final RenderBox box = keyWidget.currentContext.findRenderObject();
        setState(() {
          height = box.size.height + 90;
        });
      });

  @override
  void initState() {
    super.initState();
    calculateSizeAndPosition();
    // 发起网络请求
    _attendanceController.getVisitor();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: buildAppBar(),
        body: SafeArea(
          child: SingleChildScrollView(
            physics: BouncingScrollPhysics(),
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 20),
              child: Column(
                children: [
                  Column(
                    key: keyWidget,
                    children: [
                      VisitorCount(),
                      SizedBox(height: 25),
                      VisitorMenu(),
                      SizedBox(height: 15),
                    ],
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height - height,
                    child: Padding(
                      padding: const EdgeInsets.only(top: 10),
                      child: VisitorList(),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      );
  AppBar buildAppBar() {
    String date =
        _selectedDate != null ? _selectedDate.toString().split(" ")[0] : '今日';
    return AppBar(
      centerTitle: true,
      backgroundColor: Colors.transparent,
      title: GestureDetector(
        onTap: () async {
          var result = await showDatePicker(
            context: context,
            initialDate: _selectedDate ?? DateTime.now(),
            firstDate: DateTime(2020),
            lastDate: DateTime.now(),
          );
          if (result != null) {
            setState(() {
              _selectedDate = result;
            });
          }
        },
        child: Text(
          date,
          style: TextStyle(
            fontSize: 20,
            color: Colors.black,
          ),
        ),
      ),
      elevation: 0,
    );
  }
}
